package top.healthylife.gateway.platform.zzc.api.check;

import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Service;
import top.healthylife.basics.utils.exception.BusinessException;
import top.healthylife.gateway.core.config.BaseConfig;
import top.healthylife.gateway.core.service.CheckSecretService;
import top.healthylife.gateway.platform.zzc.api.api.ZzcBlackListSpecV3Api;
import top.healthylife.gateway.platform.zzc.api.config.SystemConfig;
import top.healthylife.gateway.platform.zzc.api.util.OutHttpUtil;

import java.util.Arrays;
import java.util.HashMap;

/**
 * @author : GaoZeXi
 * @date 2021/3/19 14:48
 */
@Service
public class CheckSecretServiceImpl implements CheckSecretService {
    @Override
    public void checkSecret(BaseConfig baseConfig) {
        SystemConfig zzcConfig = (SystemConfig) baseConfig;
        String respContext;
        HashMap<Object, Object> objectObjectHashMap = new HashMap<>(4);
        objectObjectHashMap.put("name","刘辉");
        objectObjectHashMap.put("pid","132529197905080811");
        objectObjectHashMap.put("mobile","13833312708");
        respContext = HttpRequest.post(ZzcBlackListSpecV3Api.URL_PRO)
                .header("Authorization", zzcConfig.getToken())
                .body(JSONObject.toJSONString(objectObjectHashMap))
                .execute().body();
        JSONObject jsonObject;
        try {
            jsonObject = JSONObject.parseObject(respContext);
        } catch (Exception e) {
            throw new BusinessException("账号测试不通过");
        }
        Object o = jsonObject.get(OutHttpUtil.ERROR_CODE);
        if(o!=null){
            String errorCode = o.toString();
            String errorMessage = jsonObject.get(OutHttpUtil.ERROR_MESSAGE).toString();
            if (!OutHttpUtil.ERROR_MAP.containsKey(errorCode)) {
                throw new BusinessException("账号测试不通过,"+errorMessage);
            }
            if (Arrays.asList(OutHttpUtil.TOKEN_WRONG).contains(errorCode)) {
                throw new BusinessException(OutHttpUtil.ERROR_MAP.get(errorCode));
            }
        }
    }
}
